package cn.com.qtgl.sso.oidc.model.entity;

import cn.com.qtgl.oauth2.TokenMetaData;
import cn.com.qtgl.sso.oidc.model.vo.TokenMessage;
import cn.hutool.core.bean.BeanUtil;
import cn.melonlib.authentication.model.entity.Profile;
import cn.melonlib.authentication.model.entity.Tenant;
import cn.melonlib.cores.aop.enums.AccessTokenType;
import cn.melonlib.cores.commons.comp.utils.AdvancedStringUtils;
import cn.melonlib.cores.commons.model.entity.ApiEntity;
import cn.melonlib.cores.identification.model.entity.Identification;
import cn.melonlib.cores.model.anno.ModelComment;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

@Data
@EqualsAndHashCode(callSuper = true)
@ModelComment("用户信息")
@JsonIgnoreProperties(ignoreUnknown = true)
public class SsoProfile extends Profile implements ApiEntity, Identification<String> {

    public SsoProfile(){

    }
    public SsoProfile(TokenMetaData tokenMetaData, String token, Tenant tenant){
        this.setId(tokenMetaData.getAccountOrSub());
        this.setSub(tokenMetaData.getSub());
        this.setLoginName(AdvancedStringUtils.defaultIfBlank((String)tokenMetaData.get("loginName")
                ,()->(String)tokenMetaData.get("userCode"))

        );
        this.setAccount(this.getLoginName());
        this.setName(tokenMetaData.getName());
        this.email=tokenMetaData.getEmail();
        this.tenant=tokenMetaData.getTenant();
        this.setToken(token);
        this.setTenantView(tenant);
        this.setTenant(tenant.getCode());
    }

    public SsoProfile(String id,String account, String name, String token, Tenant tenant) {
        this.setId(id);
        this.setAccount(account);
        this.setName(name);
        this.setToken(token);
        this.setTenantView(tenant);
        this.setTenant(tenant.getCode());
    }

    public SsoProfile(TokenMessage tokenMessage){
        BeanUtil.copyProperties(tokenMessage,this);
        if(StringUtils.isNotBlank(tokenMessage.getOpenId())) {
            if (StringUtils.isBlank(getId())) {
                setId(tokenMessage.getOpenId());
            }
            setLoginName(tokenMessage.getUserName());
            if (StringUtils.isBlank(getAccount())) {
                setAccount(tokenMessage.getLoginName());
            }
            this.setOpenid(tokenMessage.getOpenId());
            accessTokenType=AccessTokenType.USER;
        }else{
            accessTokenType=AccessTokenType.SYSTEM;
            this.setOpenid(tokenMessage.getClient_id());
            this.setAccount(tokenMessage.getClient_id());
            this.setLoginName(tokenMessage.getClient_id());
        }
    }


    private AccessTokenType accessTokenType=AccessTokenType.USER;

    @ModelComment("账号内部关联ID(openid)")
    private String sub;

    @ModelComment("登陆唯一标识")
    private String loginName;


    @ModelComment("该用户邮箱")
    private String email;

    @ModelComment("该用户手机 ")
    private String phone;

    @ModelComment("Favorite的应用code")
    private String favoriteApps;

    @ModelComment("Favorite的任务id")
    private String favoriteTasks;

    @ModelComment("Favorite的标签")
    private String favoriteTags;

    @ModelComment("所属单位")
    private Enterprise enterprise;

    @ModelComment("租户编码")
    private String tenant;
}
